$(document).ready(function(){
    //单选、全选
    $('table s').click(function(){
        $(this).toggleClass('selected');
    });
    $("table").on('click','tbody s',function (e) {
        e.stopPropagation();
        var item = $(this).parent().parent().parent().find('s');
        var count = 0;
        $(item).each(function () {
            if (!$(this).hasClass('selected')) {
                $(this).removeClass('selected');
                return false;
            } else {
                count++;
            }
        });
        if (count == item.length) {
            $(this).parent().parent().parent().siblings('thead').find('s').addClass('selected');
        } else {
            $(this).parent().parent().parent().siblings('thead').find('s').removeClass('selected');
        }
    });
    $("table").on('click','thead s',function (e) {
        e.stopPropagation();
        if(!$(this).hasClass('selected')){
            $('table tbody s').removeClass('selected');
        }else{
            $('table tbody s').addClass('selected');
        }
    });
    $('table').on('click','tbody tr td button',function(e){
        e.preventDefault();
    });
    //详情、修改、删除
    $(function () {
        $(".show-form table tbody").on('click', 'tr td .delete', function () {
            $('#mask-box').css('display','block').children('#remove-block').css('display','block');
        });
    });
    $('#mask-box').on('click','.queren',function(){
        $('#mask-box').css('display','none');
    });
    $('#mask-box').on('click','.cancel',function(){
        $('#mask-box').css('display','none');
    });
    //标签切换
    $('.tab').on('click','div',function(){
        if($(this).hasClass('chose-product')){
            $(this).addClass('active').siblings('div').removeClass('active');
            $('form.show-form').removeClass('hide').siblings('form').addClass('hide');
        }else if($(this).hasClass('revise')){
            $(this).addClass('active').siblings('div').removeClass('active');
            $('form.add-form').removeClass('hide').siblings('form').addClass('hide');
        }
    });
});

function getProduct(){
	var condition = $("#condition").val();
	$.ajax({
        url: "../getProductByCondition",
        type: "POST",                       //请求方式
        data:{condition:condition},
        dataType: "json",
        success: function (result,obj) {
        	$(".add-form tbody").children("tr").remove();
        	var html="";
        	var productList = result.obj
        	console.log(productList);
        	for (var int = 0; int < productList.length; int++) {
				html+="<tr data-id="+productList[int].id+"><td><s></s></td><td><div><img src='"+productList[int].photoHref+"'/></div></td><td><p>"+(productList[int].code==undefined?"":productList[int].code)+"</p><p>"+(productList[int].productName==undefined?"":productList[int].productName)+"</p><p>"+(productList[int].brandName==undefined?"":productList[int].brandName)+"</p></td><td>"+(productList[int].groupSpecValue==undefined?"":productList[int].groupSpecValue)+"</td><td>"+(productList[int].standard==undefined?"":productList[int].standard)+"</td><td>"+(productList[int].price.toFixed(2)==undefined?"":productList[int].price.toFixed(2))+"/"+(productList[int].littleUnit==undefined?"":productList[int].littleUnit)+"</td><td>"+(productList[int].orderedPrice.toFixed(2)==undefined?"":productList[int].orderedPrice.toFixed(2))+"/"+(productList[int].littleUnit==undefined?"":productList[int].littleUnit)+"</td><td>"+(productList[int].purchasePrice.toFixed(2)==undefined?"":productList[int].purchasePrice.toFixed(2))+"/"+(productList[int].littleUnit==undefined?"":productList[int].littleUnit)+"</td><td><input type='text' id='addSortNum' value='"+productList[int].mainpageSortNum+"'/></td><td><button class='detail'>详情</button><button class='add' onclick='addMainpageProduct("+productList[int].id+",this)'>添加</button></td></tr>";
			}
        	$(".add-form tbody").html(html);
        },
        fail: function (result) {
            // 此处放失败后执行的代码
        }
    });
}

/**
 * 取消首页商品设置
 */
function deleteMainpageProduct(id,that){
	var that = $(that).parent("td").parent("tr");
	$(".queren").on('click', function (e) {
	    e.stopPropagation();
	    e.preventDefault();
	    $.ajax({
	        url: "../cancelMainpageProduct",
	        type: "POST",                       //请求方式
	        data:{id:id},
	        dataType: "json",
	        success: function (result,obj) {
	        	if(result.status){
	        		that.remove();
	        		$('#mask-box').css('display','none');
	        		alertBox(1,"删除成功");
	        	} else{
	        		alertBox(0,"删除失败");
	        	}
	        },
	        fail: function (result) {
	            // 此处放失败后执行的代码
	        }
	    });
	});
}

function addMainpageProduct(id){
	var sortNum = $("#addSortNum").val();
	console.log(sortNum);
    $.ajax({
        url: "../addMainpageProduct",
        type: "POST",                       //请求方式
        data:{id:id,sortNum:sortNum},
        dataType: "json",
        success: function (result,obj) {
        	if(result.status){
        		$('#mask-box').css('display','none');
        		alertBox(1,"删除成功");
        	} else{
        		alertBox(0,"删除失败");
        	}
        },
        fail: function (result) {
            // 此处放失败后执行的代码
        }
    });
}
